package MySQL::Query::Aliases;

use strict;
use warnings;

use base qw(MySQL::Query::Base);
__PACKAGE__->mk_accessors(qw(dbh tree table cur_num));

use Carp;

sub init {
      my $self = shift;
      
      croak "No table passed to constructor" unless $self->table;
      $self->cur_num(0);
}

sub add     { push @{shift->{aliases}} , @_     }
sub count   { return $#{shift->{aliases}} + 1   }
sub list    { return shift->{aliases}           }

sub next {
      my $self = shift;
      
      if ($self->cur_num >= $self->count) {
            $self->cur_num(0);
            return;
      }
      
      my $alias = $self->list->[$self->cur_num];
      $self->cur_num($self->cur_num + 1);
	
      return $alias;
}

1;

=head1 NAME

MySQL::Query::Aliases - Alias management for one table

=head1 SYNOPSIS
      
      #creating object with table name
      my $alias = MySQL::Query::Aliases->new(table => "table_name");
      
      # adding aliases for a table
      $alias->add('alias_name');
      $alias->add('another_alias');
      
      #getting list of aliases
      my $aliases_list = $alias->list(); # ['alias_name' , 'another_alias']
      
      #iterator
      while (my $alias = $alias->next()) {
            # do whatever with $alias
      }
                 
=head1 DESCRIPTION

=head1 METHODS

=head2 new(table => )
      
      Created new object:
            my $alias = MySQL::Query::Aliases->new( table => 'table_name' );
            tree is mandatory, it should be MySQL::Query object
            dbh is mandatory only if you want to use describe_table() method
      
=head2 tree()
      
      Returns MySQL::Query object
            my $tree_object = $tables->tree;
      
=head2 parse_tree()

      Returns DBIx::MyParsePP object
            my $myparse = $tables->parse_tree;
            
=head2 dbh()

      Returns DBI handle if it was specified when constracting an object
            my $dbh = $tables->dbh;

=head2 tables()

      Returns internal tables hash, keys are table names and values are arrayrefs to aliases
            my $internal_t = $tables->tables;

=head2 tables_num()

      Returns number of tables detected
            my $tables_num = $tables->tables_num;

=head2 get_tables()

      This method should be called after constracting an object if you want
      to work with tables and their aliases. It returns table names
            my @tables = $tables->get_tables();

=head2 next_table()

      It's some form of iterator over the internal tables structure.
      while (my $table = $tables->next_table) {
            ... do something ...
      }

=head2 table_alias($tables)
      
      $tables is an arrayref to a table names.
      Returns a list of aliases
            my $aliases = $tables->table_alias(['table_name' , 'one_more_table']);

=head2 alias($table)

      Same as prev.

=head2 aliases($tables)

      $tables is an arrayref to a table names.
      The difference between table_alias() and aliases() is that table_alias() returns a list of aliases
      and aliases() return a list of list.
            my $aliases = $table->aliases(['table1' , 'table2' , 'table3']);
            #print Dumper $aliases
            #[ ['table1_alias1', 'table1_alias2'] , "" , ['table3_alias1'] ];

=head2 describe_table($table)
      
      Returns table description from database
            my $descr = $table->describe_table
      
=head1 AUTHOR

Mikhail Maluyk <mikhail.maluyk@gmail.com>
1;
